﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using WCFAlmacen.Dominio;
using System.Data.SqlClient;

namespace WCFAlmacen.Persistencia
{
    public class GRMaterialDAO
    {
        public List<GRMaterial> ListarMateriales()
        {

            SqlDataReader rdr = null;
            SqlConnection con = null;
            SqlCommand cmd = null;
            List<GRMaterial> ListMateriales = null;
            GRMaterial objMateriales = null;

            try
            {
                con = new SqlConnection(ConexionUtil.ObtenerCadena());
                con.Open();
                cmd = new SqlCommand("GRMaterial_Listar", con);
                cmd.CommandType = CommandType.StoredProcedure;
                rdr = cmd.ExecuteReader();

                if (rdr.HasRows)
                {
                    ListMateriales = new List<GRMaterial>();
                    while (rdr.Read())
                    {
                        objMateriales = new GRMaterial();
                        objMateriales.iCodMat = Convert.ToInt32(rdr["iCodMat"].ToString());
                        objMateriales.iCodSub = Convert.ToInt32(rdr["iCodSub"].ToString());
                        objMateriales.vNomMat = rdr["vNomMat"].ToString();
                        objMateriales.iCodEstMat = Convert.ToInt32(rdr["iCodEstMat"].ToString());
                        objMateriales.iCodUni = Convert.ToInt32(rdr["iCodUni"].ToString());
                        objMateriales.nCanMat = Convert.ToDouble(rdr["nCanMat"].ToString());
                        objMateriales.iCodUsu = Convert.ToInt32(rdr["iCodUsu"].ToString());
                        objMateriales.vNomTer = rdr["vNomTer"].ToString();
                        objMateriales.dtFecAct = Convert.ToDateTime(rdr["dtFecAct"].ToString());
                        ListMateriales.Add(objMateriales);
                    }
                }
            }
            catch
            {
                ListMateriales = null;
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();

                if (con.State == ConnectionState.Open)
                    con.Close();
            }
            return ListMateriales;
        }

        public GRMaterial ObtenerDatosMaterial(Int32 intCodMaterial)
        {

            SqlDataReader rdr = null;
            SqlConnection con = null;
            SqlCommand cmd = null;
            GRMaterial objMateriales = null;

            try
            {
                con = new SqlConnection(ConexionUtil.ObtenerCadena());
                con.Open();
                cmd = new SqlCommand("GRMaterial_Obtener", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@iCodMat", SqlDbType.Int).Value = intCodMaterial;
                rdr = cmd.ExecuteReader();
                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                        objMateriales = new GRMaterial();
                        objMateriales.iCodMat = Convert.ToInt32(rdr["iCodMat"].ToString());
                        objMateriales.iCodSub = Convert.ToInt32(rdr["iCodSub"].ToString());
                        objMateriales.vNomMat = rdr["vNomMat"].ToString();
                        objMateriales.iCodEstMat = Convert.ToInt32(rdr["iCodEstMat"].ToString());
                        objMateriales.iCodUni = Convert.ToInt32(rdr["iCodUni"].ToString());
                        objMateriales.nCanMat = Convert.ToDouble(rdr["nCanMat"].ToString());
                        objMateriales.iCodUsu = Convert.ToInt32(rdr["iCodUsu"].ToString());
                        objMateriales.vNomTer = rdr["vNomTer"].ToString();
                        objMateriales.dtFecAct = Convert.ToDateTime(rdr["dtFecAct"].ToString());
                    }
                }
            }
            catch
            {
                objMateriales = null;
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();

                if (con.State == ConnectionState.Open)
                    con.Close();
            }
            return objMateriales;
        }

        public List<GRMaterial> ObtenerMaterialXSubCategoria(Int32 intCodSubCategoria)
        {

            SqlDataReader rdr = null;
            SqlConnection con = null;
            SqlCommand cmd = null;
            List<GRMaterial> ListMateriales = null;
            GRMaterial objMateriales = null;

            try
            {
                con = new SqlConnection(ConexionUtil.ObtenerCadena());
                con.Open();
                cmd = new SqlCommand("GRMateriales_x_SubCategorias", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@iCodSub", SqlDbType.Int).Value = intCodSubCategoria;
                rdr = cmd.ExecuteReader();
                if (rdr.HasRows)
                {
                    ListMateriales = new List<GRMaterial>();
                    while (rdr.Read())
                    {
                        objMateriales = new GRMaterial();
                        objMateriales.iCodMat = Convert.ToInt32(rdr["iCodMat"].ToString());
                        objMateriales.iCodSub = Convert.ToInt32(rdr["iCodSub"].ToString());
                        objMateriales.vNomMat = rdr["vNomMat"].ToString();
                        objMateriales.iCodEstMat = Convert.ToInt32(rdr["iCodEstMat"].ToString());
                        objMateriales.iCodUni = Convert.ToInt32(rdr["iCodUni"].ToString());
                        objMateriales.nCanMat = Convert.ToDouble(rdr["nCanMat"].ToString());
                        objMateriales.iCodUsu = Convert.ToInt32(rdr["iCodUsu"].ToString());
                        objMateriales.vNomTer = rdr["vNomTer"].ToString();
                        objMateriales.dtFecAct = Convert.ToDateTime(rdr["dtFecAct"].ToString());
                        ListMateriales.Add(objMateriales);
                    }
                }
            }
            catch
            {
                ListMateriales = null;
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();

                if (con.State == ConnectionState.Open)
                    con.Close();
            }
            return ListMateriales;
        }
    }
}